package lib.gui;

import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.AutoTextEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import lib.classes.VerticalFieldManagerFieldSet;

public class SettingsScreen extends Page implements FieldChangeListener
{

	CheckboxField isSearchable;
	CheckboxField syncWithBBM;
	// Search queries
	AutoTextEditField searchScreenName;
	
	ObjectChoiceField searchGender;
	ObjectChoiceField searchAgeFrom;
	ObjectChoiceField searchAgeTo;
	
	// Searching by location:
	// If searchNearby is checked, the application 
	// will search based on users within the same Geographic coordinates
	// other
	CheckboxField searchNearBy;
	AutoTextEditField searchCity;
	AutoTextEditField searchProvince;
	AutoTextEditField searchCountry;
		
	ButtonField save;
	VerticalFieldManager searchLocationFields;
	public SettingsScreen() {
		super("Settings");
		
		// Profile Settings

        VerticalFieldManagerFieldSet profileCat = new VerticalFieldManagerFieldSet();
		isSearchable = new CheckboxField("Make profile searchable?", profile.isPrivate());
		syncWithBBM  = new CheckboxField("Sync profile with BBM?", profile.isSyncWithBBM());
		profileCat.add(isSearchable);profileCat.add(new SeparatorField());
		profileCat.add(syncWithBBM);profileCat.add(new SeparatorField());
		LabelField lf = new LabelField("Profile Settings");
		lf.setPadding(3, 5, 3, 5);
		add(lf);
		
		add(profileCat);
		
		//Browse Settings
        VerticalFieldManagerFieldSet browseCat = new VerticalFieldManagerFieldSet();
		
        searchScreenName = new AutoTextEditField("Screen Name: ", "", 70,EditField.FILTER_DEFAULT);
        browseCat.add(searchScreenName);
        
        searchGender = new ObjectChoiceField("Gender: ", new Object[]{"Any","Kings","Queens"});
        browseCat.add(searchGender);
        
        searchAgeFrom = new ObjectChoiceField("", new Object[]{});        
        searchAgeTo = new ObjectChoiceField("", new Object[]{});
        
        HorizontalFieldManager ageRangeField = new HorizontalFieldManager(USE_ALL_WIDTH);
        ageRangeField.add(new LabelField("Age(s): "));ageRangeField.add(searchAgeFrom);ageRangeField.add(new LabelField(" - "));ageRangeField.add(searchAgeTo);
        browseCat.add(ageRangeField);
                
        searchNearBy = new CheckboxField("Search nearby (mates within 50km) ", true);
        searchNearBy.setChangeListener(this);
        
        searchCity = new AutoTextEditField("City: ", "", 70,EditField.FILTER_DEFAULT);
    	searchProvince = new AutoTextEditField("Province: ", "", 70,EditField.FILTER_DEFAULT);
        searchCountry = new AutoTextEditField("Country: ", "", 70,EditField.FILTER_DEFAULT);
        
        searchLocationFields = new VerticalFieldManager(USE_ALL_WIDTH);
        searchLocationFields.add(searchCity);
        searchLocationFields.add(searchProvince);
        searchLocationFields.add(searchCountry);
        searchLocationFields.setEnabled(false);
        browseCat.add(searchLocationFields);
        
        add(browseCat);
        
        HorizontalFieldManager bottom = new HorizontalFieldManager(USE_ALL_WIDTH | DrawStyle.HCENTER | FIELD_HCENTER);
        save = new ButtonField("Save");
        bottom.add(save);
        bottom.setPadding(3,5, 3, 5);
        add(bottom);
        
	}
	public void fieldChanged(Field field, int context) 
	{
		if(field == this.left)
		{
			popScreen();
		}else if(field == this.right)
		{
			pushScreen(new SettingsScreen());
		}
		
		if(field == searchNearBy)
		{
			searchLocationFields.setEnabled(!searchNearBy.getChecked());
		}
	}
	
	private String[] ages()
	{
		String[] ages = new String[61-18];
		for(int i=18; i <= 60; i++)
		{
			ages[i] = i+"";
		}
		
		return ages;
	}

}